﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SuperFox.SuperServiceReference;
using System.Collections.ObjectModel;

namespace SuperFox
{
    public partial class UserProfile : UserControl
    {

        private SuperServiceClient client;

        private ObservableCollection<int> UsersIDsToGet { get; set; }
        private int UserListIndex { get; set; }
        private ObservableCollection<DbUsers> UsersCollection { get; set; }

        public UserProfile()
        {
            InitializeComponent();

            this.DataContext = (App.Current.RootVisual as MainPage).CurrentUser;

            client = ServiceConfig.Connect();
            client.GetAllUsersCompleted += new EventHandler<GetAllUsersCompletedEventArgs>(client_GetAllUsersCompleted);
            client.SaveLoginCompleted += new EventHandler<SaveLoginCompletedEventArgs>(client_SaveLoginCompleted);
            client.SaveProgramCompleted += new EventHandler<SaveProgramCompletedEventArgs>(client_SaveProgramCompleted);

            client.GetNumberOfUsersForMerchCompleted += new EventHandler<GetNumberOfUsersForMerchCompletedEventArgs>(client_GetNumberOfUsersForMerchCompleted);
            client.GetSingleUsersCompleted += new EventHandler<GetSingleUsersCompletedEventArgs>(client_GetSingleUsersCompleted);

            client.GetNumberOfUsersForMerchAsync((App.Current.RootVisual as MainPage).CurrentUser.merc_id);

            //client.GetAllUsersAsync((App.Current.RootVisual as MainPage).CurrentUser.merc_id);
        }

        void client_GetSingleUsersCompleted(object sender, GetSingleUsersCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("There was an error getting user data. Please try again");
                return;
            }

            this.UsersCollection.Add(e.Result);
            e.Result.WriteStatus = Config.WriteStatusValues.Unchanged;
            e.Result.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(Result_PropertyChanged);
            foreach (DbProducts pr in e.Result.Programs.Products)
            {
                pr.PropertyChanged +=
                    delegate
                    {
                        e.Result.WriteStatus = Config.WriteStatusValues.Changed;
                    };
            }

            this.UserListIndex++;

            if (this.UserListIndex >= this.UsersIDsToGet.Count)
            {
                // we are done
                return;
            }
            else
            {
                client.GetSingleUsersAsync(this.UsersIDsToGet[this.UserListIndex]);
            }
            
        }

        
        

        void Result_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName != "WriteStatus")
            {
                (sender as DbUsers).WriteStatus = Config.WriteStatusValues.Changed;
            }
        }

        void client_GetNumberOfUsersForMerchCompleted(object sender, GetNumberOfUsersForMerchCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                MessageBox.Show("There was an error getting user data. Please try again");
                return;
            }

            this.UsersIDsToGet = e.Result;

            this.UsersCollection = new ObservableCollection<DbUsers>();
            this.LoginsList.Users = this.UsersCollection;

            client.GetSingleUsersAsync(this.UsersIDsToGet[this.UserListIndex]);
        }

        void client_GetAllUsersCompleted(object sender, GetAllUsersCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                this.LoginsList.Users = e.Result;
            }
            else
            {
                MessageBox.Show("There was a problem loading the users logins");
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            StatusBlock.Text = "";

            //client = ServiceConfig.Connect();

            

            client.SaveLoginAsync((App.Current.RootVisual as MainPage).CurrentUser.log_password, (App.Current.RootVisual as MainPage).CurrentUser.log_id);

            if ((App.Current.RootVisual as MainPage).CurrentUser.log_caneditprogram)
            {

                DbMarchPrograms tempProgs = new DbMarchPrograms()
                {
                    merc_accountemail = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_accountemail,
                    merc_name = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_name,
                    merc_programmanageremail = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_programmanageremail,
                    merc_id = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_id,
                    merc_marketingmanager = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_programmanageremail,
                    merc_deniedmessage = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_deniedmessage,
                    merc_confirmedmessage = (App.Current.RootVisual as MainPage).CurrentUser.Programs.merc_confirmedmessage
                };

                
                
                client.SaveProgramAsync(tempProgs);

                this.LoginsList.Save();
            }
        }

        void client_SaveProgramCompleted(object sender, SaveProgramCompletedEventArgs e)
        {
            StatusBorder.Visibility = System.Windows.Visibility.Visible;

            if (e.Error == null)
            {
                if (e.Result)
                {
                    StatusBlock.Text += " Program Information Saved";
                    return;
                }
            }

            StatusBlock.Text += " ERROR: Program Information Not Saved";
        }

        void client_SaveLoginCompleted(object sender, SaveLoginCompletedEventArgs e)
        {
            StatusBorder.Visibility = System.Windows.Visibility.Visible;

            if (e.Error == null)
            {
                if (e.Result)
                {
                    StatusBlock.Text += " User Profile Information Saved";
                    return;
                }
            }

            StatusBlock.Text += " ERROR: Profile Information Not Saved";
        }

   

       
    }
}
